package cn.ssdcc.dkserver.mapper;

import cn.ssdcc.dkserver.entity.ImMsg;
import cn.ssdcc.dkserver.entity.Music;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ImMapper {
    @Insert("CREATE TABLE IF NOT EXISTS `immsg` (\n" +
            "\t`id` integer NOT NULL AUTO_INCREMENT COMMENT '主键',\n" +
            "\t`time` varchar(100) NOT NULL COMMENT '时间',\n" +
            "\t`type` varchar(100) NOT NULL COMMENT '类型',\n" +
            "\t`text` varchar(10000) NOT NULL COMMENT '内容',\n" +
            "\t`userid` varchar(100) NOT NULL COMMENT '上传者',\n" +
            "\tPRIMARY KEY (`id`)\n" +
            ")")
    int createImMsg();

    @Insert("INSERT INTO dk.immsg\n" +
            "(`time`, `type`, text, userid)\n" +
            "VALUES(#{imMsg.time}, #{imMsg.type}, #{imMsg.text}, #{imMsg.userid});\n")
    int insertImMsg(@Param("imMsg") ImMsg imMsg);

    @Select("SELECT *\n" +
            "FROM (\n" +
            "\tSELECT i.id, i.`time`, i.`type`, i.`text`, userid, u.name AS username, u.img AS userimg\n" +
            "\tFROM immsg i LEFT JOIN `user` u ON i.userid = u.id\n" +
            "\tWHERE i.`time` > #{time}\n" +
            "\tORDER BY i.`time` DESC\n" +
            "\tLIMIT 100\n" +
            ") t\n" +
            "ORDER BY `time`")
    List<ImMsg> getnewImMsg(@Param("time") String time);

    @Select("SELECT i.id, i.`time`, i.`type`, i.`text`, userid\n" +
            "\t\t, u.name AS username, u.img AS userimg\n" +
            "\tFROM immsg i\n" +
            "\t\tLEFT JOIN `user` u ON i.userid = u.id\n" +
            "\tWHERE i.`time` < #{time}\n" +
            "\tORDER BY i.`time` DESC\n" +
            "\tLIMIT 100")
    List<ImMsg> getnewImMsgHistory(@Param("time") String time);

}
